﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows;

namespace Hugo
{
    public abstract class UIOption
    {
        public int ID { get; set; }
        public String Name { get; set; }
        public BitmapSource IconSource { get; set; }

        public UIOption(String name, BitmapSource iconSource)
            : this(name, iconSource, true) { }

        private static int lastID = 0;

        public UIOption(String name, BitmapSource iconSource, bool canTranslate)
        {
            this.CanTranslate = canTranslate;
            this.ID = ++lastID;
            this.Name = name;
            this.IconSource = iconSource;
        }

        private System.Windows.Controls.Canvas c;
        private bool isSelected = false;
        private int cursorCountDown = 0;

        public virtual bool applyCursorToContent(System.Windows.Point p)
        {
            if (r == null) return false;
            bool isOverSomething = false;


                double rX = Canvas.GetLeft(r);// + currentpos;
                double rY = Canvas.GetTop(r);// -verticalBuffer;
                double rX2 = rX + r.ActualWidth;
                double rY2 = rY + r.ActualHeight;// +verticalBuffer * 2;

                if (rX < p.X && rY < p.Y && rX2 > p.X && rY2 > p.Y)
                {
                    isOverSomething = true;

                    if (!isSelected)
                    {
                        isSelected = true;

                        Sounds.playSound(Sounds.Sound.Navigate);

                        HoldProgressIndicator.getInstance().setProgressLocation(new Point(Canvas.GetLeft(r), Canvas.GetTop(r)));

                        //startFlashing(selectedRectangle);

                        cursorCountDown = GlobalSettings.HOLD_TIME;
                    }

                    cursorCountDown--;


                    HoldProgressIndicator.getInstance().setProgressAmount(GlobalSettings.HOLD_TIME - cursorCountDown, GlobalSettings.HOLD_TIME);

                    if (cursorCountDown == 0)
                    {
                        this.wasSelected();
                    }
            }

            if (!isOverSomething)
            {
                HoldProgressIndicator.getInstance().setProgressAmount(0, 1);
                isSelected = false;

                /*
                if (flashAnimationStoryboard != null)
                {
                    //flashAnimationStoryboard.Stop(this.MainWindow);
                    this.selectedRectangle = null;
                    this.selectedOptionName = null;
                }
                 */
            }

            return isOverSomething;
        }

        Rectangle r;
        public bool CanTranslate;

        public Image getIconImage()
        {
            return Util.getBitmapImage(this.IconSource);
        }

        public virtual System.Windows.Controls.Canvas getContent() {
            if (c == null)
            {
                c = new System.Windows.Controls.Canvas();
                r = Util.getSymbol(IconSource);
                r.Width = GlobalSettings.ICON_WIDTH_SELECTED;
                r.Height = GlobalSettings.ICON_HEIGHT_SELECTED;
                c.Children.Add(r);
                Canvas.SetLeft(r, GlobalSettings.EffectiveScreenMiddleX - GlobalSettings.ICON_WIDTH_SELECTED / 2);
                Canvas.SetTop(r, GlobalSettings.EffectiveScreenMiddleY - GlobalSettings.ICON_WIDTH_SELECTED /2);
            }
            return c;
        }

        public abstract void wasSelected();

        public virtual String getDescription()
        {
            return this.Name;
        }

    }
}
